var express = require('express');
var app = express();
//相对于当前的node_moduels文件夹
var mysqlCon = require('./util/mysqlCon.js')
var db = require('./util/mysqlExecute.js')
var router = require("./util/router.js");
var formidable = require('formidable');
var session = require('express-session')
// var network = require('./public/js/controller/network.js')
//session
app.use(session({
    secret: 'keyboard cat',
    resave: true,
    saveUninitialized: true
}));
//设置模板引擎
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
//静态
app.use(express.static("./public"));
/*----------------------------------------登录逻辑---------------------------*/
//跳转到登录页
app.use("/login", function (req, res ) {
    // res.send("htllo")
    res.render("login",{

    })
})

//路由到人力资源档案登记
app.use("/views/addEmployee", function (req, res ) {
    // res.send("htllo")
    res.render("addEmployee",{

    })
})

//人力资源档案查询
app.use("/views/selectEmployee", function (req, res ) {
    // res.send("htllo")
    res.render("selectEmployee",{

    })
})

//人力资源档案查询结果
app.use("/views/resultEmployee", function (req, res ) {
    res.render("resultEmployee",{

    })
})
//人力资源档案修改
app.use("/views/updateEmployee", function (req, res ) {
    // res.send("htllo")
    console.log('tiao')
    res.render("updateEmployee",{

    })
})

//登录接口
app.use("/api/v1/admin/personLogin",function (req, res) {
    router.personLogin(req,res)
})
app.use("/api/v1/admin/salaryLogin",function (req, res) {
    router.salaryLogin(req, res)

})
//验证是否登录
// app.use("/",function(req,res,next){
//     if(req.session.login == "1"){
//         // res.send("欢迎" + req.session.name);
//         // res.json({"status": 200})
//         next()
//     }else{
//         //没有登录
//         res.send("你还没有登录登陆");
//         // res.json({"status": 403, "message": "err"});
//     }
// });

/*------------------------------------------接受请求-------------------------*/
//第一个参数为路径，第二个参数为函数 自动执行

//插入档案接口  档案登记（人事经理 人事专员）
app.use("/api/v1/admin/addEmployee",function (req, res) {
    router.addEmployee(req,res)
})


//查询档案接口 （人事经理 人事专员）
app.use('/api/v1/admin/selectEmployee', function (req,res) {
    router.selectEmployee(req,res)
})
//按条件查询档案(人事经理 人事专员)
app.use('/api/v1/admin/switchSelEmployee', function (req,res) {
    router.switchSelEmployee(req,res)
})

//修改档案（人事经理）
//更新员工数据 档案编号、所属机构和职位不能修改，其他信息均可修改  档案复核接口（人事经理）
app.use('/api/v1/admin/updateEmployee', function (req,res) {
    router.updateEmployee(req,res)
})

//删除档案（人事经理）
app.use('/api/v1/admin/deleteEmployee', function (req,res) {
    console.log("删除档案")
    router.deleteEmployee(req,res)
})

//复核档案（人事经理）
app.use('/api/v1/admin/checkEmployee', function (req,res) {
    router.checkEmployee(req,res)
})

//获取所有机构名
app.use('/api/v1/admin/selectOrg', function (req,res) {
    router.selectOrg(req,res)
})

//获取所有职称名
app.use('/api/v1/admin/selectPosition', function (req,res) {
    router.selectPosition(req,res)
})

/*------------------------------------------接受路由——-------------------------*/
//跳转到首页
app.use("/personIndex", function (req, res ) {

    // res.send("htllo")
    res.render("personIndex",{
        Id: req.session.Id,
        name: req.session.name,
        position: req.session.position
    })
})
app.use("/salaryIndex", function (req, res ) {

    // res.send("htllo")
    res.render("salaryIndex",{
        Id: req.session.Id,
        name: req.session.name,
        position: req.session.position
    })
})

/*---------------------------------------------人力资源档案管理--------------------------------------*/
// 插入用户----
app.use('/addUser', function (req,res) {
    var con = mysqlCon.getCon();

    var  addSql = 'INSERT INTO user(Id,name,password,role,creatTime) VALUES(0,?,?,?,?)';
    var  addSqlParams = ['碧峰3', '2','2', '2017-12-05', '10001'];

    db.addSql(con, addSql, addSqlParams, function (result) {
        console.log(result)
        res.json(result)
    })
})
//插入员工-----档案登记接口(人事专员)
app.use('/addEmployee', function (req,res) {
    var con = mysqlCon.getCon();
    var  addSql = 'INSERT INTO employees(id ,name, age, sex, email,phone, qq, telephone, address, postCode, nationality, birthPlace, birthday, nation, faith, politicalStatus, idcard, soSeNum, edubg, major, bank, accountNumber, hobby, resume, familyRelative, remark, thirdOrg, professional, creatTime, registrant, isDelete, isCheck, fileNum) VALUES(0,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
    var  addSqlParams = ['碧峰2', 20, 0, '123456@174.com','84913246','123456800', '13420120011','广东省湛江市','511212','中国','广州市', '1997-10-06', '汉', '无', '团员', '44018115000925133', '44018115000925133','本科', '软件工程', '1255631231561315615616', '123456789514563', '唱歌', '无' , '无', '没有', 13, 3, '2017-12-16', 1, 0, 0, 201701112101];


    db.addSql(con, addSql, addSqlParams, function (result) {
        console.log(result)
        res.send(result)
    })
})
app.use('/selectEmployee', function (req,res) {

    var con = mysqlCon.getCon();
    var sql = "select * from employees"
    db.selectSql(con, sql, function (result) {
        console.log(result)
        res.json(result)
    })
})

//更新员工数据 档案编号、所属机构和职位不能修改，其他信息均可修改  档案复核接口（人事经理）
//获取登记人 档案号 id
app.use('/updateEmployee', function (req,res) {

    var con = mysqlCon.getCon();
    var modSql = 'UPDATE employees SET name = ?,  age = ?, sex = ?, email = ?, phone = ?, qq = ?, telephone = ?, address = ?, postCode = ?, nationality = ?, birthPlace= ?, birthday = ?, nation = ?, faith = ?, politicalStatus = ?, idcard = ?, soSeNum = ?, edubg = ?, major = ?, bank = ?, accountNumber = ?, hobby = ?, resume = ?, familyRelative = ?, remark = ?  ,isDelete = ?, isCheck = ? WHERE id = 10016';
    var modSqlParams = ['牛群2', '23', '男', '1175166300@qq.com', '13420124594', '1175166300', '11754166300', '南沙区', '511475', '中国', '广州市南沙区冬虫真', '1997-09-25', '汉', '无', '无','4401811977', '440181199709251533', '本科','软件工程', '4546453435453', '4313123132165165156', '写代码', 'adasdas实习经验', '双亲', '标记', '1' ,'1'];


    db.updateSql(con, modSql, modSqlParams, function (result) {
        console.log(result)
        res.send(result)
    })
})

// var mysql  = require('mysql');
//
// var connection = mysql.createConnection({
//     host     : 'localhost',
//     user     : 'root',
//     password : '',
//     port: '3306',
//     database: 'personnel',
// });
// var modSql = 'UPDATE employees SET name = ?,  age = ?, sex = ?, email = ?, phone = ?, qq = ?, telephone = ?, address = ?, postCode = ?, nationality = ?, birthPlace= ?, birthday = ?, nation = ?, faith = ?, politicalStatus = ?, idcard = ?, soSeNum = ?, edubg = ?, major = ?, bank = ?, accountNumber = ?, hobby = ?, resume = ?, familyRelative = ?, remark = ?  ,isDelete = ?, isCheck = ? WHERE id = 10016';
// var modSqlParams = ['牛群2', '21', '男', '1175166300@qq.com', '13420124594', '1175166300', '11754166300', '南沙区', '511475', '中国', '广州市南沙区冬虫真', '1997-09-25', '汉', '无', '无','4401811977', '440181199709251533', '本科','软件工程', '4546453435453', '4313123132165165156', '写代码', 'adasdas实习经验', '双亲', '标记', '1' ,'1'];
// //改
// connection.query(modSql,modSqlParams,function (err, result) {
//     if(err){
//         console.log('[UPDATE ERROR] - ',err.message);
//         return;
//     }
//     console.log('--------------------------UPDATE----------------------------');
//     console.log('UPDATE affectedRows',result);
//     console.log('-----------------------------------------------------------------\n\n');
// });

//删除员工档案----删除档案接口(人事经理)
app.use('/deleteEmployee', function (req,res) {
    var con = mysqlCon.getCon();
    var delSql  = 'UPDATE employees SET isDelete = ? where id=10020';
    var delSqlParams = [1];

    db.updateSql(con, delSql, delSqlParams, function (result) {
        console.log(result)
        if(result.changedRows == 1){
            res.json({result, "message": "success", "status": "200"})
        }else{
            res.json({"message": "err", "status": "404", "data": null})
        }

    })
})
// var modSql = 'DELETE FROM employees where id=10017';
// var modSqlParams = [1];
// //改
// connection.query(modSql,modSqlParams,function (err, result) {
//     if(err){
//         console.log('[UPDATE ERROR] - ',err.message);
//         return;
//     }
//     console.log('--------------------------UPDATE----------------------------');
//     console.log('UPDATE affectedRows',result.affectedRows);
//     console.log('-----------------------------------------------------------------\n\n');
// });

/*---------------------------------------------薪酬管理--------------------------------------*/

//路由到薪酬登记
app.use("/views/addSalary", function (req, res ) {
    // res.send("htllo")
    res.render("addSalary",{

    })
})
//薪酬查询
app.use("/views/selectSalary", function (req, res ) {
    // res.send("htllo")
    res.render("selectSalary",{

    })
})
//薪酬查询结果
app.use("/views/resultSalary", function (req, res ) {
    // res.send("htllo")
    res.render("resultSalary",{

    })
})
//薪酬修改
app.use("/views/updateSalary", function (req, res ) {
    console.log(`薪酬修改: ${req.query.id} ${req}`)
    res.render("updateSalary",{

    })
})


//插入薪酬标准 薪酬标准登记（薪酬专员 薪酬经理）
app.use('/api/v1/admin/addSalary', function (req,res) {
    router.addSalary(req,res)

})

//复核薪酬标准（薪酬经理）
app.use('/api/v1/admin/checkSalary', function (req,res) {
    router.checkSalary(req,res)
})
//查询薪酬标准
app.use('/api/v1/admin/selectSalary', function (req,res) {
    router.selectSalary(req,res)

})
//按照id查询薪酬标准
app.use('/api/v1/admin/selectSalaryID', function (req,res) {
    router.selectSalaryID(req,res)

})
//删除薪酬标准
app.use('/api/v1/admin/deleteSalary', function (req,res) {
    router.deleteSalary(req,res)

})


app.listen(3000)